import java.util.Arrays;

public class insertSort {
    public static void sort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int i = 1; i < arr.length; i++) { // 从0 ~ i做到有序
            for (int j = i - 1; j >=0 && arr[j] > arr[j+1]; j--) {
                swap(arr, j, j+1);
            }
        }
        
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    
    public static void main(String[] args) {
        int[] arr = {2,4,1,5,6,2,3,1};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
    
}
